package com.example.yml0601;

import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.yml.R;

public class Yuan0601 extends AppCompatActivity implements View.OnClickListener {


    private Button button1;
    private ProgressBar pb_1;
    private TextView tv_1;
    private Button button2;
    private ProgressBar pb_2;
    private TextView tv_2;
    private Button button3;
    private ProgressBar pb_3;
    private TextView tv_3;
    private Button button4;
    private ProgressBar pb_4;
    private TextView tv_4;
    private Button button5;
    private ProgressBar pb_5;
    private TextView tv_5;
    private Button button6;
    private ProgressBar pb_6;
    private TextView tv_6;
    private Button button7;
    private ProgressBar pb_7;
    private TextView tv_7;
    private Button button8;
    private ProgressBar pb_8;
    private TextView tv_8;
    private Button button9;
    private ProgressBar pb_9;
    private TextView tv_9;
    private Button button10;
    private ProgressBar pb_10;
    private TextView tv_10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yuan0601);
        initView();

    }

    private void initView() {
        button1 = (Button) findViewById(R.id.button1);
        pb_1 = (ProgressBar) findViewById(R.id.pb_1);
        tv_1 = (TextView) findViewById(R.id.tv_1);
        button2 = (Button) findViewById(R.id.button2);
        pb_2 = (ProgressBar) findViewById(R.id.pb_2);
        tv_2 = (TextView) findViewById(R.id.tv_2);
        button3 = (Button) findViewById(R.id.button3);
        pb_3 = (ProgressBar) findViewById(R.id.pb_3);
        tv_3 = (TextView) findViewById(R.id.tv_3);
        button4 = (Button) findViewById(R.id.button4);
        pb_4 = (ProgressBar) findViewById(R.id.pb_4);
        tv_4 = (TextView) findViewById(R.id.tv_4);
        button5 = (Button) findViewById(R.id.button5);
        pb_5 = (ProgressBar) findViewById(R.id.pb_5);
        tv_5 = (TextView) findViewById(R.id.tv_5);
        button6 = (Button) findViewById(R.id.button6);
        pb_6 = (ProgressBar) findViewById(R.id.pb_6);
        tv_6 = (TextView) findViewById(R.id.tv_6);
        button7 = (Button) findViewById(R.id.button7);
        pb_7 = (ProgressBar) findViewById(R.id.pb_7);
        tv_7 = (TextView) findViewById(R.id.tv_7);
        button8 = (Button) findViewById(R.id.button8);
        pb_8 = (ProgressBar) findViewById(R.id.pb_8);
        tv_8 = (TextView) findViewById(R.id.tv_8);
        button9 = (Button) findViewById(R.id.button9);
        pb_9 = (ProgressBar) findViewById(R.id.pb_9);
        tv_9 = (TextView) findViewById(R.id.tv_9);
        button10 = (Button) findViewById(R.id.button10);
        pb_10 = (ProgressBar) findViewById(R.id.pb_10);
        tv_10 = (TextView) findViewById(R.id.tv_10);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
        button7.setOnClickListener(this);
        button8.setOnClickListener(this);
        button9.setOnClickListener(this);
        button10.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                MyTask myTask1 = new MyTask(button1, pb_1, tv_1);
                myTask1.execute();
                break;
            case R.id.button2:
                MyTask myTask2 = new MyTask(button2, pb_2, tv_2);
                myTask2.execute();
                break;
            case R.id.button3:
                MyTask myTask3 = new MyTask(button3, pb_3, tv_3);
                myTask3.execute();
                break;
            case R.id.button4:
                MyTask myTask4 = new MyTask(button4, pb_4, tv_4);
                myTask4.execute();
                break;
            case R.id.button5:
                MyTask myTask5 = new MyTask(button5, pb_5, tv_5);
                myTask5.execute();
                break;
            case R.id.button6:
                MyTask myTask6 = new MyTask(button6, pb_6, tv_6);
                myTask6.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                break;
            case R.id.button7:
                MyTask myTask7 = new MyTask(button7, pb_7, tv_7);
                myTask7.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                break;
            case R.id.button8:
                MyTask myTask8 = new MyTask(button8, pb_8, tv_8);
                myTask8.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                break;
            case R.id.button9:
                MyTask myTask9 = new MyTask(button9, pb_9, tv_9);
                myTask9.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                break;
            case R.id.button10:
                MyTask myTask10 = new MyTask(button10, pb_10, tv_10);
                myTask10.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                break;
        }
    }

    public class MyTask extends AsyncTask<Void, Integer, Void> {
        Button btn;
        ProgressBar br;
        TextView tv;

        public MyTask(Button btn, ProgressBar br, TextView tv) {
            this.btn = btn;
            this.br = br;
            this.tv = tv;
        }


        @Override
        protected Void doInBackground(Void... voids) {
            for (int i = 1; i <= 100; i++) {
                SystemClock.sleep(100);
                publishProgress(i);
            }
            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            btn.setEnabled(false);
            br.setVisibility(View.VISIBLE);
            tv.setVisibility(View.VISIBLE);
            btn.setTextColor(Color.parseColor("#00574B"));
            btn.setText("正在下载");
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            btn.setEnabled(true);
            br.setVisibility(View.GONE);
            tv.setVisibility(View.GONE);
            btn.setTextColor(Color.parseColor("#3B55E7"));
            btn.setText("下载完成");
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            int i = values[0];
            br.setProgress(i);
            tv.setText(i + "%");
        }
    }

}
